package net;

import java.io.*;
import java.net.Socket;
class pos implements Serializable{
    public double x;
    public double y;

    pos(double x,double y) {
        this.x=x;
        this.y=y;
    }

    public double dist(pos p) {
        double f1=this.x-p.x;
        f1=f1*f1;
        double f2=this.y-p.y;
        f2=f2*f2;
        return Math.sqrt(f1+f2);
    }

    public String toString() {
        return "("+Double.toString(x)+","+Double.toString(y)+")";
    }
}

abstract class GeometricObject implements Serializable {
    public abstract double getC();
    public abstract double getS();
}

class Rectangle extends GeometricObject implements Serializable{
    private double dx;
    private double dy;

    private pos p1;
    private pos p2;

    public Rectangle(pos p1, pos p2) {
        this.p1=p1;
        this.p2=p2;
        this.dx=p2.x-p1.x;
        this.dy=p2.y-p1.y;
    }

    public double getC() {
        return dx*2+dy*2;
    }

    public double getS() {
        return dx*dy;
    }

    public String toString() {
        return "Rectangle: "+Double.toString(dx)+","+Double.toString(dy);
    }
}
class Circle extends GeometricObject implements Serializable{
    public pos p;
    public double r;
    public Circle(pos p, double r) {
        this.p=p;
        this.r=r;
    }

    public double getS() {
        return Math.PI*r*r;
    }

    public double getC() {
        return 2*Math.PI*r;
    }

    public String toString() {
        return "Circle: "+p.toString()+","+Double.toString(r);
    }
}

class Triangle extends GeometricObject implements Serializable{
    private double a;
    private double b;
    private double c;

    private pos p1;
    private pos p2;
    private pos p3;

    public Triangle(pos p1, pos p2, pos p3) {
        this.p1=p1;
        this.p2=p2;
        this.p3=p3;
        this.a=p1.dist(p2);
        this.b=p2.dist(p3);
        this.c=p1.dist(p3);
    }

    public double getC() {
        return a+b+c;
    }

    public double getS() {
        double p=a+b+c/2;
        return Math.sqrt(p*(p-a)*(p-b)*(p-c));
    }

    public String toString() {
        return "Triangle: "+Double.toString(a)+","+Double.toString(b)+","+Double.toString(c);
    }
}

public class client {
    public static void main(String[] args) throws IOException {
        Socket s = new Socket("localhost", 1010);
        var sb= new ObjectOutputStream(s.getOutputStream());
        Rectangle rec=new Rectangle(new pos(3,3),new pos(4,4));
        Circle cir=new Circle(new pos(3,3),4);
        Triangle tri=new Triangle(new pos(3,3),new pos(3,3),new pos(3,3));
        sb.writeObject((Object)rec);
        sb.flush();
        sb.writeObject((Object)tri);
        sb.flush();
        sb.writeObject((Object)cir);
        sb.flush();

        System.out.println("添加元素完成");

        try {
			Thread.sleep(1000);
		} 
        catch (InterruptedException e1) {}

        sb.writeObject((Object)"cha");
        sb.flush();
        System.out.println("开始获取");
        sb.writeObject((Object)"END"); //结束指令，让服务端break
        sb.flush();

        ObjectInputStream br=new ObjectInputStream(s.getInputStream());
        String info;
		try {
			info = (String)br.readObject();
			System.out.println(info);
		} catch (ClassNotFoundException e) {}
        s.close();
    }
}

